package cn.zzf.core;

import cn.zzf.mapper.ClassroomInfoMapper;
import cn.zzf.mapper.ClassroomStatusMapper;
import cn.zzf.mapper.ExperimentLessonMapper;
import cn.zzf.mapper.TeacherLessonMapper;
import cn.zzf.po.ClassroomInfo;
import cn.zzf.po.ClassroomStatus;
import cn.zzf.po.ExperimentLesson;
import cn.zzf.po.TeacherLesson;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author GaoFeng2017
 * @date 2018-04-06 23:41:25
 **/

public class ClassroomStatusWriteToDb {
    static List<String> list = new ArrayList<>(5);

    static {
        list.add("1-2");
        list.add("3-4");
        list.add("5-6");
        list.add("7-8");
        list.add("9-10");
    }
    public void writeAll() throws IOException {
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder()
                .build(Resources.getResourceAsStream("mybatis.xml"));

            /*dao类*/
        SqlSession sqlSession = sessionFactory.openSession();
        ClassroomStatusMapper classroomStatusMapper = sqlSession.getMapper(ClassroomStatusMapper.class);
        ClassroomInfoMapper classroomInfoMapper = sqlSession.getMapper(ClassroomInfoMapper.class);
        List<ClassroomInfo> classroomInfos = classroomInfoMapper.selectAll();
        List<ClassroomStatus> classroomStatuses = new ArrayList<>(450000);
        for (ClassroomInfo classroomInfo : classroomInfos) {
            for (int i = 1;i <= 17;i++) {
                for (int j = 1;j <= 7;j++) {
                    for (int k = 0;k < 5;k++) {
                        ClassroomStatus classroomStatus = new ClassroomStatus();
                        classroomStatus.setClassroomId(classroomInfo.getId());
                        classroomStatus.setPeriod(list.get(k));
                        classroomStatus.setStatusCode((byte) 0);
                        classroomStatus.setWeek((byte) i);
                        classroomStatus.setWeekday((byte) j);
                        classroomStatuses.add(classroomStatus);

                    }
                }
            }

            classroomStatusMapper.insertList(classroomStatuses);
            sqlSession.commit();
            classroomStatuses.clear();
        }
        sqlSession.close();
    }

    public void updateAll() throws IOException {
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder()
                .build(Resources.getResourceAsStream("mybatis.xml"));

            /*dao类*/
        SqlSession sqlSession = sessionFactory.openSession();
        ClassroomStatusMapper classroomStatusMapper = sqlSession.getMapper(ClassroomStatusMapper.class);
        TeacherLessonMapper teacherLessonMapper = sqlSession.getMapper(TeacherLessonMapper.class);
        ExperimentLessonMapper experimentLessonMapper = sqlSession.getMapper(ExperimentLessonMapper.class);

        List<TeacherLesson> teacherLessons = teacherLessonMapper.selectAll();
        List<ExperimentLesson> experimentLessons = experimentLessonMapper.selectAll();

        List<ClassroomStatus> classroomStatuses = new ArrayList<>(100000);

        /*for (TeacherLesson teacherLesson : teacherLessons) {
            ClassroomStatus classroomStatus = new ClassroomStatus();
            classroomStatus.setWeekday(teacherLesson.getWeekday());
            classroomStatus.setWeek(teacherLesson.getWeek());
            classroomStatus.setPeriod((byte) (list.indexOf(teacherLesson.getPeriod()) + 1));
            classroomStatuses.add(classroomStatus);
        }

        for (ExperimentLesson experimentLesson : experimentLessons) {
            ClassroomStatus classroomStatus = new ClassroomStatus();
            classroomStatus.setWeekday(experimentLesson.getWeekday());
            classroomStatus.setWeek(experimentLesson.getWeek());
            classroomStatus.setPeriod((byte) (list.indexOf(experimentLesson.getPeriod()) + 1));
            classroomStatuses.add(classroomStatus);
        }

        System.out.println(classroomStatuses.size());
        for (ClassroomStatus classroomStatus : classroomStatuses) {
            classroomStatusMapper.updateAll(classroomStatus);
        }*/

        sqlSession.commit();
        sqlSession.close();
    }
}
